package images

import (
	"context"

	"server/internal/common/biz"
	"server/internal/svc"
	"server/internal/types"
	"server/internal/utils"

	"github.com/zeromicro/go-zero/core/logx"
)

type ImageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewImageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ImageLogic {
	return &ImageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

/*
	{
	  "Id": "sha256:3b25b682ea82b2db3cc4fd48db818be788ee3f902ac7378090cf2624ec2442df",
	  "RepoTags": [
	    "nginx:latest"
	  ],
	  "RepoDigests": [
	    "nginx@sha256:28402db69fec7c17e179ea87882667f1e054391138f77ffaf0c3eb388efc3ffb"
	  ],
	  "Parent": "",
	  "Comment": "buildkit.dockerfile.v0",
	  "Created": "2024-10-02T17:55:35Z",
	  "DockerVersion": "",
	  "Author": "",
	  "Config": {
	    "Hostname": "",
	    "Domainname": "",
	    "User": "",
	    "AttachStdin": false,
	    "AttachStdout": false,
	    "AttachStderr": false,
	    "ExposedPorts": {
	      "80/tcp": {

	      }
	    },
	    "Tty": false,
	    "OpenStdin": false,
	    "StdinOnce": false,
	    "Env": [
	      "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
	      "NGINX_VERSION=1.27.2",
	      "NJS_VERSION=0.8.6",
	      "NJS_RELEASE=1~bookworm",
	      "PKG_RELEASE=1~bookworm",
	      "DYNPKG_RELEASE=1~bookworm"
	    ],
	    "Cmd": [
	      "nginx",
	      "-g",
	      "daemon off;"
	    ],
	    "ArgsEscaped": true,
	    "Image": "",
	    "Volumes": null,
	    "WorkingDir": "",
	    "Entrypoint": [
	      "/docker-entrypoint.sh"
	    ],
	    "OnBuild": null,
	    "Labels": {
	      "maintainer": "NGINX Docker Maintainers \\u003cdocker-maint@nginx.com\\u003e"
	    },
	    "StopSignal": "SIGQUIT"
	  },
	  "Architecture": "amd64",
	  "Os": "linux",
	  "Size": 191670474,
	  "GraphDriver": {
	    "Data": {
	      "LowerDir": "/var/lib/docker/overlay2/e102a60262ca82478a6de577428d1f75fc1a107937e08401adbeb37cb9ab282c/diff:/var/lib/docker/overlay2/fbadd6dc7a29d7d651bef471408897ca80c677a03b827c425d2142db2afe5fd9/diff:/var/lib/docker/overlay2/92b8a0308dbad923141177f00d8097b560481d351176a5f5652dd8f5b8ec813a/diff:/var/lib/docker/overlay2/8176dfd359e098759c6256fc800e3c3546bc6f01e4672d04a0faf144310f9420/diff:/var/lib/docker/overlay2/e00b770320320641a882726bb819ac8f426be6eab6a7fe2ffe1ff3f2fb6b3962/diff:/var/lib/docker/overlay2/d294a30c0ab41e0759c68ceca6040ae6296e83b1ddccfb898ee9d72ed8b6efc6/diff",
	      "MergedDir": "/var/lib/docker/overlay2/6a1c03d41854b14409f62c745c9247b3c5dd34e30f5107729ef5ed68d4baa5dd/merged",
	      "UpperDir": "/var/lib/docker/overlay2/6a1c03d41854b14409f62c745c9247b3c5dd34e30f5107729ef5ed68d4baa5dd/diff",
	      "WorkDir": "/var/lib/docker/overlay2/6a1c03d41854b14409f62c745c9247b3c5dd34e30f5107729ef5ed68d4baa5dd/work"
	    },
	    "Name": "overlay2"
	  },
	  "RootFS": {
	    "Type": "layers",
	    "Layers": [
	      "sha256:98b5f35ea9d3eca6ed1881b5fe5d1e02024e1450822879e4c13bb48c9386d0ad",
	      "sha256:b33db0c3c3a85f397c49b1bf862e0472bb39388bd7102c743660c9a22a124597",
	      "sha256:63d7ce983cd5c7593c2e2467d6d998bb78ddbc2f98ec5fed7f62d730a7b05a0c",
	      "sha256:296af1bd28443744e7092db4d896e9fda5fc63685ce2fcd4e9377d349dd99cc2",
	      "sha256:8ce189049cb55c3084f8d48f513a7a6879d668d9e5bd2d4446e3e7ef39ffee60",
	      "sha256:6ac729401225c94b52d2714419ebdb0b802d25a838d87498b47f6c5d1ce05963",
	      "sha256:e4e9e9ad93c28c67ad8b77938a1d7b49278edb000c5c26c87da1e8a4495862ad"
	    ]
	  },
	  "Metadata": {
	    "LastTagTime": "0001-01-01T00:00:00Z"
	  }
	}
*/
func (l *ImageLogic) Image(req *types.ImageReq) (resp *types.ImageResp, err error) {

	image, _, err := l.svcCtx.DockerCli.ImageInspectWithRaw(l.ctx, req.Id)
	if err != nil {
		l.Logger.Errorf("ImageInspectWithRaw error: %s", err.Error())
		return nil, biz.ImageGetError
	}

	l.Logger.Debug(utils.PrintLog("image-detail", image))
	return &types.ImageResp{
		ImagesResp: types.ImagesResp{
			Id:       image.ID[7:],
			RepoTags: image.RepoTags,
			Created:  utils.FormatIso(image.Created),
			Size:     image.Size,
		},
		ExposedPorts: func() []string {
			portSet := image.Config.ExposedPorts
			ports := make([]string, 0, len(portSet))
			for k := range portSet {
				ports = append(ports, k.Port())
			}
			return ports
		}(),
		Env:         image.Config.Env,
		Cmd:         image.Config.Cmd,
		EntryPoint:  image.Config.Entrypoint,
		Architecure: image.Architecture,
		Os:          image.Os,
		Detail:      image,
	}, nil
}
